# -*- coding: utf-8 -*-

import sys

from PyQt4 import QtGui
from PyQt4 import QtCore

'''
email client gui
'''


class GUI(QtGui.QWidget):

    def __init__(self):
        super(GUI, self).__init__()
        self.initUI_index()
        #self.initUI_get()

    def initUI_index(self):
        lbl_email = QtGui.QLabel(u'邮箱：')
        lbl_passwd = QtGui.QLabel(u'密码：')

        self.edit_email = QtGui.QLineEdit()
        self.edit_passwd = QtGui.QLineEdit()

        self.btn_submit = QtGui.QPushButton(u'登陆', self)

        self.connect(self.btn_submit, QtCore.SIGNAL('clicked()'), self.btn_submit_clicked)


        self.grid = QtGui.QGridLayout()
        self.grid.setSpacing(10)

        self.grid.addWidget(lbl_email, 1, 0)
        self.grid.addWidget(self.edit_email, 1, 1)

        self.grid.addWidget(lbl_passwd, 2, 0)
        self.grid.addWidget(self.edit_passwd, 2, 1)

        self.grid.addWidget(self.btn_submit, 3, 1)

        self.setLayout(self.grid)

        self.setWindowTitle('Email Client')
        self.setWindowIcon(QtGui.QIcon('Mail.png'))
        self.resize(350, 300)

        self.show()

    def initUI_send(self):
        while self.grid.count():
            item = self.grid.takeAt(0)
            widget = item.widget()
            # if widget has some id attributes you need to
            # save in a list to maintain order, you can do that here
            # i.e.:   aList.append(widget.someId)
            widget.deleteLater()
        self.setWindowTitle('Email Client')
        self.setWindowIcon(QtGui.QIcon('Mail.png'))
        self.resize(1000, 500)

        btn_send = QtGui.QPushButton(u'发送邮件', self)

        self.connect(btn_send, QtCore.SIGNAL('clicked()'), self.btn_send_clicked)

        lbl_to = QtGui.QLabel(u'收信人：')
        lbl_subject = QtGui.QLabel(u'主题：')
        lbl_content = QtGui.QLabel(u'内容：')

        self.edit_to = QtGui.QLineEdit()
        self.edit_subject = QtGui.QLineEdit()
        self.edit_content = QtGui.QTextEdit()

        self.grid.setSpacing(10)

        self.grid.addWidget(lbl_to, 1, 0)
        self.grid.addWidget(self.edit_to, 1, 1, 1, 4)

        self.grid.addWidget(lbl_subject, 2, 0)
        self.grid.addWidget(self.edit_subject, 2, 1, 1, 4)

        self.grid.addWidget(lbl_content, 3, 0, 1, 1)
        self.grid.addWidget(self.edit_content, 3, 1, 5, 4)

        self.grid.addWidget(btn_send, 8, 2, 1, 1)  

        self.setLayout(self.grid)

    def btn_submit_clicked(self):
        self.from_addr = self.edit_email.text()
        print self.from_addr
        self.passwd = self.edit_passwd.text()
        print self.passwd
        self.delete()
        self.initUI_send()

    def btn_send_clicked(self):
        import text as text
        self.to_addr = self.edit_to.text()
        self.subject = unicode(self.edit_subject.text().toUtf8(), 'utf-8', 'ignore')
        self.content = unicode(self.edit_content.toPlainText().toUtf8(), 'utf-8', 'ignore')
        print self.from_addr,self.to_addr,self.passwd,self.subject,self.content
        text.send_gui(self.from_addr, self.to_addr, self.passwd, self.subject, self.content)
        #text.get_gui(self.to_addr, self.passwd)

    def delete(self):
         while self.grid.count():
            item = self.grid.takeAt(0)
            widget = item.widget()
            # if widget has some id attributes you need to
            # save in a list to maintain order, you can do that here
            # i.e.:   aList.append(widget.someId)
            widget.deleteLater()

def main():
	app = QtGui.QApplication(sys.argv)
	w = GUI()
	w.show()
	sys.exit(app.exec_())

if __name__ == '__main__':
	main()